<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 19-3-6
 * Time: 下午2:21
 */

namespace api\modules\icbcapi\controllers;


use common\components\icbcapi\Config;
use common\components\icbcapi\sdk\DefaultIcbcClient;
use common\components\icbcapi\sdk\IcbcConstants;
use common\components\tools\Model;
use common\models\RaexinCheckLog;
use wodrow\yii\rest\ApiException;
use wodrow\yii\rest\Controller;

class RaexinController extends Controller
{
    /**
     * 融安E信查询信息
     * @param string $acc_no 身份证
     * @param int $re_check 重新查询
     * @return mixed
     */
    public function actionCheck($acc_no, $re_check = 0)
    {
        $r = [];
        $serviceUrl = Config::$serviceUrl;
        $bankId = Config::$bankId;
        $appId = Config::$appId;
        $log = RaexinCheckLog::findOne(['service_url' => $serviceUrl, 'bank_id' => $bankId, 'app_id' => $appId, 'acc_no' => $acc_no]);
        if (!$log){
            $log = new RaexinCheckLog();
            $log->service_url = $serviceUrl;
            $log->bank_id = $bankId;
            $log->app_id = $appId;
            $log->acc_no = $acc_no;
            $re_check = 1;
        }
        $request = array(
            "serviceUrl" => $serviceUrl, // 使用api接口地址
            "method" => 'POST', // 请求方法，只能是POST或GET
            "isNeedEncrypt" => false, // 是否需要加密
            "extraParams" => null,//其他参数
            "biz_content" => array( //业务数据
                "bankId" => $bankId,
                "data" => array("accNo" => $acc_no),
                "source" => "3",
                "time" => date("YmdHis")."00000",
                "type" => "1",
                "userid" => "",
                "ver" => "3",
            )
        );
        $privateKey = Config::$privateKey;
        $icbcPublicKey = Config::$icbcPublicKey;
        $client = new DefaultIcbcClient($appId, //APP的编号,应用在API开放平台注册时生成
            $privateKey, //APP应用私钥
            IcbcConstants::$SIGN_TYPE_RSA,//签名类型，’CA’-工行颁发的证书认证;’RSA’表示RSAWithSha1;’RSA2’表示RSAWithSha256;缺省为RSA
            '',//字符集，仅支持UTF-8,可填空‘’
            '',//请求参数格式，仅支持json，可填空‘’
            $icbcPublicKey, //网关公钥，必填
            '', //AES加密密钥，缺省为空‘’
            '', //加密类型，当前仅支持AES加密，需要按照接口类型是否需要加密来设置，缺省为空‘’
            '', //当签名类型为CA时，通过该字段上送证书公钥，缺省为空
            ''); //当签名类型为CA时，通过该字段上送证书密码，缺省为空
        try {
            if ($re_check){
                $msgId = time()."000".rand(1000, 9999);
                $resp = $client->execute($request, $msgId, ''); //执行调用
                $resp = json_decode($resp, true);
                if (!is_array($resp)){
                    $respArr = json_decode($resp, true);
                }else{
                    $respArr = $resp;
                    $resp = json_encode($resp, JSON_UNESCAPED_UNICODE);
                }
                $log->msg_id = $msgId;
                $log->resp_data = $resp;
                if (!isset($respArr['respCd'])){
                    throw new ApiException(201903111442, "查询失败:".$resp);
                }
                if (isset($respArr['respCd'])){
                    $log->resp_code = $respArr['respCd'];
                }
                if (isset($respArr['type'])){
                    $log->resp_type = $respArr['type'];
                }
                if (isset($respArr['msg'])){
                    $log->resp_msg = $respArr['msg'];
                }
                $r['plat_back'] = $respArr;
            }
            $log->private_key = $privateKey;
            $log->icbc_public_key = $icbcPublicKey;
            $log->created_at = time();
            $log->req_data = json_encode($request, JSON_UNESCAPED_UNICODE);
            if (!$log->save()){
                throw new ApiException(201903111451, Model::getModelError($log));
            }
            $r['log'] = [
                'acc_no' => $acc_no,
                'resp_code' => $log->resp_code,
            ];
            return $r;
        } catch (\Exception $e) {//捕获异常
            echo 'Exception:' . $e->getMessage() . "\n";
            exit;
        }
    }
}